import { computed, onMounted, ref } from 'vue'
import { PageApi } from './api'
import type { TableListItem } from './api'

/**
 * 获取页面数据
 */
export function usePageList() {
    const formValues = ref<Record<string, any>>({
        name: ''
    })
    const pageList = ref<TableListItem[]>()

    onMounted(() => {
        getPageList()
    })

    async function getPageList() {
        const res = await PageApi.getPageList({
            ...formValues.value,
            limit: 100_000,
            page: 1
        })

        pageList.value = res.records
    }

    return {
        formValues,
        pageList: computed(() => {
            const arr = pageList.value
            const name = (formValues.value.name || '').trim()
            return name ? arr?.filter((item) => item.name.includes(name)) : arr
        }),
        getPageList
    }
}
